package com.powermgr.boringtest.task.impl;

import com.powermgr.boringtest.bean.PrData;
import com.powermgr.boringtest.task.PrTask;
import com.powermgr.boringtest.utils.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

import static com.powermgr.boringtest.utils.ConfigUtils.*;


/**
 * DownLoadNoPrTask
 *
 * @author qiuxiangdong
 * @date 2023/9/23 14:41
 */
@Log4j2
public class DownLoadOldPrTask implements PrTask<PrData> {
    @Override
    public boolean execute(PrData prData) {
        log.info("SN- " + prData.getSn_id() + ": " + "DownLoadOldPrTask START");
        log.info("SN- " + prData.getSn_id() + ": " + "*版本和用例已经下载过了，直接解压刷机执行");
        prData.setVersion_url("Repeat");
        FileZipUtils.unTarGzVersion(prData.getVersion_name(), prData.getSn_id());
        if (!prData.getXts_url().isEmpty() &&TypeUtils.IsXTS(prData.getType())){
            log.info("SN- " + prData.getSn_id() + ": " + "2.找XTS用例");
            if (FileZipUtils.unTarGzXts(prData.getXts_name(), prData.getSn_id())){
                EmailUtils.SendEmail(prData, UN_TAR_GZ_FALSE);
                prData.setEnd_time(ReadWriteUtils.getPrTime());
                return false;
            }
        }
        if (prData.getXts_url().isEmpty() && TypeUtils.IsXTS(prData.getType())){
            log.info("SN- " + prData.getSn_id() + ": " + "2.Pr编译结果中没有 dayu200_xts，直接拷贝本地用例执行");
            try {
                File file = new File(DAILY + File.separator + "suites");
                if (file.exists()){
                    FileUtils.copyDirectory(file,
                            new File(SN + File.separator + prData.getSn_id() +  File.separator + "pr" + File.separator + "xts"
                                    + File.separator + "suites"));
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (!prData.getTdd_url().isEmpty() &&TypeUtils.IsTDD(prData.getType())){
            log.info("SN- " + prData.getSn_id() + ": " + "2.找TDD用例");
            if (FileZipUtils.unTarGzTdd(prData.getTdd_name(), prData.getSn_id())){
                EmailUtils.SendEmail(prData, UN_TAR_GZ_FALSE);
                prData.setEnd_time(ReadWriteUtils.getPrTime());
                return false;
            }
        }
        if (prData.getTdd_url().isEmpty() && TypeUtils.IsTDD(prData.getType())){
            log.info("SN- " + prData.getSn_id() + ": " + "2.Pr编译结果中没有 dayu200_tdd，直接拷贝本地用例执行");
            try {
                File file = new File(DAILY + File.separator + "tests");
                if (file.exists()){
                    FileUtils.copyDirectory(file,
                            new File(SN + File.separator + prData.getSn_id() +  File.separator + "pr"
                                    + File.separator + "tdd" + File.separator + "tests"));
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        log.info("SN- " + prData.getSn_id() + ": " + "DownLoadOldPrTask END");
        return true;
    }
}